package com.petshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.petshop.mapper.ProductsMapper;
import com.petshop.pojo.Products;
import com.petshop.service.ProductsService;
import com.petshop.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 20289
 * @description 针对表【products(存储商品信息)】的数据库操作Service实现
 * @createDate 2025-02-27 11:17:00
 */
@Service
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements ProductsService {
    @Autowired
    private ProductsMapper productsMapper;

    /**
     * 上架一个新的商品
     * 根据名字判断商品是否存在
     * 已经存在
     * 商品不存在，
     * 添加一条新的记录
     *
     * @param products
     * @return
     */
    @Override
    public Result insertProduct(Products products) {
        LambdaQueryWrapper<Products> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Products::getName, products.getName());
        Long count = productsMapper.selectCount(lambdaQueryWrapper);
        //库存中有该商品，显示商品已存在，请修改商品库存
        if (count > 0) {
            return Result.error("商品已经存在！请修改库存");
        }
        //库存中没有该商品，添加一条新的商品记录
        productsMapper.insert(products);
        return Result.ok(products);
    }

    /**
     * 下架一个商品
     *
     * @param productId
     * @return
     */
    @Override
    public Result outProduct(Integer productId) {
        QueryWrapper<Products> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId);
        Long count = productsMapper.selectCount(queryWrapper);
        //库存中没有该商品
        if (count == 0) {
            return Result.error("商品已经下架");
        }
        //存在商品,直接下架
        productsMapper.deleteById(productId);
        return Result.ok("删除成功");
    }

    /**
     * 批量下架商品
     *
     * @param idList
     * @return
     */
    @Override
    public Result outBatchProduct(ArrayList<Integer> idList) {
        int i = productsMapper.deleteBatchIds(idList);
        if (i > 0) {
            return Result.error("已经删除" + i + "个商品！");
        }
        return Result.ok("批量删除失败！");
    }

    /**
     * 修改商品信息
     *
     * @param products
     * @return
     */
    @Override
    public Result updateProduct(Products products) {
        Integer version = productsMapper.selectById(products.getProductId()).getVersion();
        //加上乐观锁
        products.setVersion(version);
        //更新修改时间
        products.setUpdatedAt(new Date());
        productsMapper.updateById(products);
        return Result.ok("修改成功");
    }

    /**
     * 根据商品id查询商品信息
     *
     * @param productId
     * @return
     */
    @Override
    public Result queryProductById(Integer productId) {
        QueryWrapper<Products> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId);
        Long count = productsMapper.selectCount(queryWrapper);
        //库存中没有该商品
        if (count == 0) {
            return Result.error("库存中没有该商品");
        }
        //库存存在该商品，显示所有信息
        List<Products> list = productsMapper.selectList(queryWrapper);
        return Result.ok(list);
    }

    /**
     * 仅仅查询第一页商品
     *
     * @param name
     * @param category
     * @param productPerPage
     * @return
     */
    @Override
    public Result queryFirstProduct(String name, String category, Integer pageCount, Integer productPerPage) {
        Page<Products> page = new Page<>(1, productPerPage);
        Page<Products> productsPage;
        LambdaQueryWrapper<Products> lambdaQueryWrapper
                = new LambdaQueryWrapper<>();
        if (category.isEmpty() && name.isEmpty()) {
            //名字和类别都没传入，则无条件分页查询
            productsPage = productsMapper.selectPage(page, null);
        } else if (category.isEmpty()) {
            //仅仅传入名字，根据名字模糊分页查询
            lambdaQueryWrapper
                    .like(Products::getName, name);
            productsPage = productsMapper.selectPage(page, lambdaQueryWrapper);
        } else if (name.isEmpty()) {
            //仅仅传入类别，根据类别精确分页查询
            lambdaQueryWrapper
                    .eq(Products::getCategory, category);
            productsPage = productsMapper.selectPage(page, lambdaQueryWrapper);
        } else {
            lambdaQueryWrapper.like(Products::getName, name)
                    .eq(Products::getCategory, category);
            productsPage = productsMapper.selectPage(page, lambdaQueryWrapper);
        }
        return Result.ok(productsPage);
    }

    /**
     * 查询商品列表，
     * name和category都是空的时候，返回所有商品；
     * 否则返回模糊name且等于category的商品
     *
     * @param name
     * @param category
     * @return
     */
    @Override
    public Result queryProduct(String name, String category, Integer pageCount, Integer productPerPage) {
        Page<Products> page = new Page<>(pageCount, productPerPage);
        LambdaQueryWrapper<Products> lambdaQueryWrapper
                = new LambdaQueryWrapper<>();
        Page<Products> productsPage;
        if (category.isEmpty() && name.isEmpty()) {
            //名字和类别都没传入，则无条件分页查询
            productsPage = productsMapper.selectPage(page, null);
        } else if (category.isEmpty()) {
            //仅仅传入名字，根据名字模糊分页查询
            lambdaQueryWrapper
                    .like(Products::getName, name);
            productsPage = productsMapper.selectPage(page, lambdaQueryWrapper);
        } else if (name.isEmpty()) {
            //仅仅传入类别，根据类别精确分页查询
            lambdaQueryWrapper
                    .eq(Products::getCategory, category);
            productsPage = productsMapper.selectPage(page, lambdaQueryWrapper);
        } else {
            lambdaQueryWrapper.like(Products::getName, name)
                    .eq(Products::getCategory, category);
            productsPage = productsMapper.selectPage(page, lambdaQueryWrapper);
        }
        return Result.ok(productsPage);
    }

}




